from fastapi import FastAPI
from fastapi_offline import FastAPIOffline
from fastapi.staticfiles import StaticFiles
from config import get_config
from app.log import register_log
from app.event import register_event
from app.exception import register_exception
from app.middleware import register_middleware
from router import api_router

def create_app() -> FastAPI:
    settings = get_config()
    # 创建应用对象
    app = FastAPIOffline(
        debug = settings.DEBUG,
        title = settings.TITLE,
        description = settings.DESCP,
        docs_url = settings.DOC_URL,
        version  = settings.VERSION
    )
    # 设置运行日志
    register_log(app)
    # 设置系统事件
    register_event(app)
    # 定义全局异常处理
    register_exception(app)
    # 设置中间件
    register_middleware(app)
    # 加载路由
    app.include_router(api_router)
    # 挂在静态文件夹
    app.mount("/www", StaticFiles(directory=settings.WEB_PATH), name="www")
    
    return app